//
//  MineStratrgyTableViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/20.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "MineStratrgyTableViewController.h"
#import "FMDatabase.h"
#import "HomeDetailModel.h"
#import "UIImageView+WebCache.h"
#import "HomeBaseDetailViewController.h"
#import "MineStrategyCell.h"

@interface MineStratrgyTableViewController ()

@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)FMDatabase *db;

@end

@implementation MineStratrgyTableViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[MineStrategyCell class] forCellReuseIdentifier:@"cell"];
    [self searchCollectionList];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.tableFooterView = view;
    
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refreshCollectionData) name:@"添加攻略收藏" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshCollectionData
{
    [self.dataSource removeAllObjects];
    [self searchCollectionList];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineStrategyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.detailModel = self.dataSource[indexPath.row];
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeBaseDetailViewController *baseDVC = [[HomeBaseDetailViewController alloc]init];
    HomeDetailModel *model = self.dataSource[indexPath.row];
    baseDVC.ID = [model.ID stringValue];
    [self.navigationController pushViewController:baseDVC animated:YES];
}

#pragma mark - 编辑删除

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteCollectionData:self.dataSource[indexPath.row]];
    [_dataSource removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark - 查找数据库

- (void)searchCollectionList
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:@"Mine.db"];
    self.db = [FMDatabase databaseWithPath:path];

    if (![_db open]) {
        NSLog(@"打开数据库失败");
    }
    
    FMResultSet *rs = [self.db executeQuery:@"SELECT * FROM MineStrategyList"];
    while ([rs next]) {
        HomeDetailModel *detailModel = [[HomeDetailModel alloc]init];
        detailModel.ID = [NSNumber numberWithInt:[rs intForColumn:@"id"]];
        detailModel.cover_image_url = [rs stringForColumn:@"Cover_image_url"];
        detailModel.title = [rs stringForColumn:@"Title"];
        detailModel.content_html = [rs stringForColumn:@"Content_html"];
        [self.dataSource addObject:detailModel];
    }
    [rs close];
}

- (void)deleteCollectionData:(HomeDetailModel *)detailModel
{
    [_db executeUpdate:@"delete from MineStrategyList where id = ?",detailModel.ID];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
